package monitor

import (
	"fmt"
	"gluenet/pkg/apis"
	"gluenet/pkg/meta"
	"time"
)

func ListInstance(c *apis.GlueContext) {
	var (
		err error
		ins *meta.MetricsInstancesList
	)

	kind := c.WebContext.URLParam("object")

	if err = c.DataBase.MetricsInstancesList(kind, &ins); err != nil {
		c.Logger.Errorf("query metrics guids %v", err)
		c.WebContext.JSON(apis.ResponseReqErr(fmt.Errorf("query metrics guids %v", err), nil))
		return
	}

	var list []interface{}
	switch kind {
	case apis.ObjectApplication:
		var title = []string{"guid", "name", "platform", "version", "describe", "containers", "used_cores", "avg_mem_used", "error_logs_count", "create_time", "containers_detail"}
		var lines, id = make([]interface{}, len(ins.GuidList.(map[string]map[string]interface{}))), 0
		for k, _ := range ins.GuidList.(map[string]map[string]interface{}) {
			detail := ins.GuidList.(map[string]map[string]interface{})[k]["containers"].(map[string]interface{})
			cts := len(detail)
			ins.GuidList.(map[string]map[string]interface{})[k]["used_cores"] = 45
			ins.GuidList.(map[string]map[string]interface{})[k]["containers_detail"] = detail
			ins.GuidList.(map[string]map[string]interface{})[k]["containers"] = cts
			ins.GuidList.(map[string]map[string]interface{})[k]["avg_mem_used"] = 1024 * 1024 * 1024
			ins.GuidList.(map[string]map[string]interface{})[k]["error_logs_count"] = 10
			ins.GuidList.(map[string]map[string]interface{})[k]["create_time"] = time.Now().Unix()
			line, i := make([]interface{}, len(ins.GuidList.(map[string]map[string]interface{})[k])+1), 1
			line[0] = k
			for _, v := range title {
				if v == "guid" {
					continue
				}
				line[i] = ins.GuidList.(map[string]map[string]interface{})[k][v]
				i++
			}
			lines[id] = line
			id++
		}
		list = make([]interface{}, len(lines)+1)
		list[0] = title
		for k, v := range lines {
			list[k+1] = v
		}
	case apis.ObjectMachine:
		var title = []string{"guid", "name", "platform", "area", "hosts", "cpu_usage", "cores", "mem_size", "mem_usage", "running_time", "hosts_detail"}
		var lines, id = make([]interface{}, len(ins.GuidList.(map[string]map[string]interface{}))), 0
		for k, _ := range ins.GuidList.(map[string]map[string]interface{}) {
			detail := ins.GuidList.(map[string]map[string]interface{})[k]["hosts"].(map[string][]interface{})
			hts := len(detail)
			ins.GuidList.(map[string]map[string]interface{})[k]["cores"] = 96
			ins.GuidList.(map[string]map[string]interface{})[k]["cpu_usage"] = 15
			ins.GuidList.(map[string]map[string]interface{})[k]["hosts_detail"] = detail
			ins.GuidList.(map[string]map[string]interface{})[k]["hosts"] = hts
			ins.GuidList.(map[string]map[string]interface{})[k]["mem_size"] = 1024 * 1024 * 1024 * 256
			ins.GuidList.(map[string]map[string]interface{})[k]["mem_usage"] = 20
			//ins.GuidList.(map[string]map[string]interface{})[k]["os"] = "centos"
			ins.GuidList.(map[string]map[string]interface{})[k]["running_time"] = 3600 * 24 * 30
			line, i := make([]interface{}, len(ins.GuidList.(map[string]map[string]interface{})[k])+1), 1
			line[0] = k
			for _, v := range title {
				if v == "guid" {
					continue
				}
				line[i] = ins.GuidList.(map[string]map[string]interface{})[k][v]
				i++
			}
			lines[id] = line
			id++
		}
		list = make([]interface{}, len(lines)+1)
		list[0] = title
		for k, v := range lines {
			list[k+1] = v
		}
	}

	c.WebContext.JSON(apis.ResponseOK(list))
}
